package com.fsp.blog.controller;

import com.fsp.blog.common.aop.LogAnnotation;
import com.fsp.blog.common.cache.Cache;
import com.fsp.blog.service.ArticleService;
import com.fsp.blog.vo.ArticleVo;
import com.fsp.blog.vo.Result;
import com.fsp.blog.vo.params.ArticleParam;
import com.fsp.blog.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author fushengping
 * @className ArticleController
 * @description
 * @date 2021/11/4 23:18
 */
@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 首页文章列表
     * @param pageParams
     * @return
     */
    @PostMapping
    @LogAnnotation(module="文章", operation = "获取文章列表")
    @Cache(expire = 5 * 60 * 1000,name = "listArticle") //分页数据太多，也可缓存
    public Result listArticle(@RequestBody PageParams pageParams) {
        //页面接收的数据
        return articleService.listArticlesPage(pageParams);
    }

    /**
     * 最热文章
     * @return
     */
    @PostMapping("/hot")
    @Cache(expire = 5 * 60 * 1000,name = "hot_article")
    public Result hotArticle() {
        int limit = 5;
        //页面接收的数据
        return articleService.hotArticle(limit);
    }

    @PostMapping("/new")
    @Cache(expire = 5 * 60 * 1000,name = "new_article")
    public Result newArticle() {
        int limit = 5;
        //页面接收的数据
        return articleService.newArticle(limit);
    }

    /**
     * 文章归档
     * @return
     */
    @PostMapping("/listArchives")
    public Result listArchives() {
        //页面接收的数据
        return articleService.listArchives();
    }

    /**
     * 文章详情
     * @param id
     * @return
     */
    @PostMapping("/view/{id}")
    public Result findArticleById(@PathVariable("id") Long id) {
        return articleService.findArticleById(id);
    }

    /**
     * 发布文章接口
     * @param articleParam
     * @return
     */
    @PostMapping("/publish")
    public Result publish(@RequestBody ArticleParam articleParam) {
        return articleService.publish(articleParam);
    }
}
